package root.electronclass.electronclass.grade.curriculum

import android.os.Bundle
import android.view.View
import root.electronclass.electronclass.adapter.GridSpacingItemDecoration
import root.electronclass.electronclass.common.base.BasicAdapter
import root.electronclass.electronclass.common.base.BasicDialogFragment
import root.electronclass.electronclass.common.base.ViewBindingFragment
import root.electronclass.electronclass.databinding.FragmentClassCurriculumSignDialogBinding
import root.electronclass.electronclass.databinding.FragmentClassCurriculumSignDialogItemBinding
import root.electronclass.electronclass.grade.model.ClassCurriculumItem
import root.electronclass.electronclass.mvp.GlobalParameter
import root.electronclass.electronclass.other.WebFragment

/**
 *
 * @author Lokiy
 * @date 2023/3/27 23:00
 */
class ClassCurriculumSignDialog : BasicDialogFragment<FragmentClassCurriculumSignDialogBinding>() {
    private var selectPosition: Int = 0

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        val curriculumList: List<ClassCurriculumItem> = arguments?.getParcelableArrayList(ViewBindingFragment.EXTRA_DATA) ?: return
        binding.recycler.addItemDecoration(GridSpacingItemDecoration(2, 24, false))
        binding.recycler.adapter = BasicAdapter<ClassCurriculumItem, FragmentClassCurriculumSignDialogItemBinding> { bindingHolder, item ->
            bindingHolder.binding.itemSubject.text = item.subject
            bindingHolder.binding.itemTime.text = item.showTime
            bindingHolder.binding.root.isActivated = selectPosition == bindingHolder.adapterPosition
            bindingHolder.binding.root.setOnClickListener {
                selectPosition = bindingHolder.adapterPosition
                notifyChanged(list)
            }
        }.apply {
            refresh(curriculumList.mapIndexed { index, classCurriculumItem ->
                classCurriculumItem.position = index + 1
                classCurriculumItem
            }.filter { it.status == 0 })
        }

        onOkClick = {
            WebFragment.startWeb(
                requireActivity(), WebFragment.CLASS_SIGN_IN.format(
                    GlobalParameter.getEcardInfo().classId, curriculumList[selectPosition].id
                )
            )
            dismiss()
        }
    }
}